<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-row :gutter="15">
        <el-col :span="8">
          <el-form-item label="猪舍" prop="houseId">
            <el-select v-model="dataForm.houseId" placeholder="猪舍" clearable>
              <el-option
                  v-for="item in houses"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="类型" prop="type">
            <el-select v-model="dataForm.type" placeholder="类型">
              <el-option label="种猪" value="种猪"></el-option>
              <el-option label="生长猪" value="生长猪"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="销售日期" prop="salesDate">
            <el-date-picker
                v-model="dataForm.salesDate"
                align="right"
                type="date"
                placeholder="销售日期"
                :picker-options="pickerOptions"
                value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="销售类型" prop="salesType">
            <el-select v-model="dataForm.salesType" placeholder="类型">
              <el-option label="正常销售" value="正常销售"></el-option>
              <el-option label="淘汰销售" value="淘汰销售"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="栏位" prop="grid">
            <el-input v-model="dataForm.grid" placeholder="栏位"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="耳号" prop="earmark">
            <el-input v-model="dataForm.earmark" placeholder="耳号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单价" prop="unitFee">
            <el-input v-model="dataForm.unitFee" placeholder="单价"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单位" prop="unit">
            <el-input v-model="dataForm.unit" placeholder="单位"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="重量" prop="weight">
            <el-input v-model="dataForm.weight" placeholder="重量"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="数量" prop="mounts">
            <el-input v-model="dataForm.mounts" placeholder="数量"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="总价" prop="price">
            <el-input v-model="dataForm.price" placeholder="总价"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="操作人" prop="operatorName">
            <el-input v-model="dataForm.operatorName" placeholder="操作人"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="原因" prop="reason">
            <el-input v-model="dataForm.reason" placeholder="原因"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
/* eslint-disable */
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        houseId: '',
        houseName: '',
        type: '',
        salesDate: '',
        grid: '',
        price: '',
        unitFee: '',
        unit: '',
        earmark: '',
        weight: '',
        remark: '',
        mounts: '',
        reason: '',
        operatorName: '',
        salesType: '',
      },
      houses: []
    }
  },
  computed: {
    dataRule() {
      return {
        houseId: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        houseName: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        type: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        salesDate: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.getHouses()
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    getHouses() {
      let that = this;
      this.$http.get(`/basic/house/all`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.houses = res.data
      }).catch(() => {
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/sales/sales/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sales/sales/', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false})
  }
}
</script>
